﻿#pragma once
#include"RBTree.h"
namespace bit
{
	template<class K>
	class set
	{
		// 仿函数（函数对象）
		// 功能：提取key值
		// operator()接收一个K类型的参数，返回这个k就行（即key）
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public://对于set而言 只会用到红黑树的const_iterator 和 const_reverse_iterator
		typedef typename RBTree<K, const K, SetKeyOfT>::const_iterator iterator;
		typedef typename RBTree<K, const K, SetKeyOfT>::const_iterator const_iterator;

		typedef typename RBTree<K, const K, SetKeyOfT>::const_reverse_iterator reverse_iterator;
		typedef typename RBTree<K, const K, SetKeyOfT>::const_reverse_iterator const_reverse_iterator;



		//复用
		iterator begin()
		{
			return _t.begin();
		}

		//同理
		iterator end()
		{
			return _t.end();
		}

		const_iterator cbegin() const
		{
			return _t.cbegin();
		}

		//同理
		const_iterator cend() const
		{
			return _t.cend();
		}

		//同理
		reverse_iterator rbegin()
		{
			return _t.rbegin();
		}

		//同理 
		reverse_iterator rend()
		{
			return _t.rend();
		}

		//同理 
		const_reverse_iterator crbegin() const
		{
			return _t.crbegin();
		}
		const_reverse_iterator crend() const
		{
			return _t.crend();
		}

		//同理
		pair<iterator, bool> insert(const K& key)
		{
			return _t.Insert(key);
		}
		//同理
		iterator find(const K& key)
		{
			return _t.Find(key);
		}

	private://所以map的成员变量->一个红黑树类的对象
		RBTree<K, const K, SetKeyOfT> _t;
	};


	//------------------------------------------------------------------
	//测试用例
	//测试迭代器是否达到无法修改K值的效果

	//测试set用iterator 
	//效果：K无法修改
	void test_set1()
	{
		set<int> s;
		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
		for (auto e : a)
		{
			s.insert(e);
		}

		set<int>::iterator it = s.begin();
		while (it != s.end())
		{

			//*it += 100; 接触注释会报错 因为set的iterater和const_iterater都无法修改K

			cout << *it << " ";
			++it;
		}
		cout << endl;
	}


	//测试set用const_iterator 
	//效果：K无法修改
	void test_set2()
	{
		set<int> s;
		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
		for (auto e : a)
		{
			s.insert(e);
		}

		set<int>::const_iterator it = s.begin();
		while (it != s.end())
		{

			//*it += 100;接触注释会报错 因为set的iterater和const_iterater都无法修改K

			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	//测试set用reverse_iterator
	//效果：倒着遍历，K无法修改
	void test_set3()
	{
		set<int> s;
		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
		for (auto e : a)
		{
			s.insert(e);
		}

		set<int>::reverse_iterator it = s.rbegin();
		while (it != s.rend())
		{

			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	//测试set用const_reverse_iterator
	//效果：倒着遍历，K无法修改
	void test_set4()
	{
		set<int> s;
		int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
		for (auto e : a)
		{
			s.insert(e);
		}

		set<int>::const_reverse_iterator it = s.crbegin();
		while (it != s.crend())
		{

			cout << *it << " ";
			++it;
		}
		cout << endl;
	}


};